package com.lhkj.ct.admin.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.lhkj.ct.base.annntation.Log;
import com.lhkj.ct.base.enums.ActionType;
import com.lhkj.ct.base.model.Option;
import com.lhkj.ct.base.model.dto.PageQuery;
import com.lhkj.ct.base.model.ro.ActionResult;
import com.lhkj.ct.base.validator.group.ValGroups;
import com.lhkj.ct.meta.admin.model.dto.RoleForm;
import com.lhkj.ct.meta.admin.model.entity.TblRole;
import com.lhkj.ct.meta.admin.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * <p>
 *      角色管理
 * </p>
 *
 * @author jobob
 */
@RestController
@RequestMapping("/api/system/role")
@ApiSupport(order = 3)
@Api(tags = "03.角色接口")
//@RequiresRoles(value = {SystemConstants.ADMIN_ROLE_CODE})
@Validated
public class SysRoleController {

    @Resource
    private SysRoleService sysRoleService;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "角色列表", notes = "分页查询")
    @GetMapping("list")
    ////@RequiresPermissions(value = "system:role:list")
    public ActionResult<IPage<TblRole>> list(TblRole menu, PageQuery pageQuery) {
        return ActionResult.success(sysRoleService.getRolePage(menu, pageQuery));
    }

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "角色下拉框列表", notes = "分页查询")
    @GetMapping("options")
    ////@RequiresPermissions(value = "system:role:list")
    public ActionResult<List<Option<String>>> options(TblRole role) {
        return ActionResult.success(sysRoleService.listRoleOptions(role));
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "查看", notes = "查看角色详情包含角色权限id")
    @GetMapping("get/{id}")
    ////@RequiresPermissions(value = {"system:role:edit", "system:role:get"}, logical = Logical.OR)
    public ActionResult<RoleForm> get(@PathVariable String id) {
        return ActionResult.success(sysRoleService.getRoleForm(id));
    }

    @ApiOperationSupport(order = 5)
    @PostMapping("insert")
    @ApiOperation(value = "新增", notes = "新增角色")
    @Log(title = "角色管理", actionType = ActionType.INSERT)
    ////@RequiresPermissions(value = "system:role:insert")
    public ActionResult<Void> insert(@Validated(ValGroups.Insert.class) @RequestBody RoleForm entity) {
        return ActionResult.judge(sysRoleService.saveRole(entity));
    }

    @ApiOperationSupport(order = 6)
    @PutMapping("update")
    @ApiOperation(value = "编辑", notes = "编辑角色")
    @Log(title = "角色管理", actionType = ActionType.UPDATE)
    ////@RequiresPermissions(value = "system:role:edit")
    public ActionResult<Void> update(@Validated(ValGroups.Update.class) @RequestBody RoleForm entity) {
        return ActionResult.judge(sysRoleService.saveRole(entity));
    }

    @ApiOperationSupport(order = 7)
    @DeleteMapping("delete")
    @ApiOperation(value = "删除", notes = "删除角色")
    @Log(title = "角色管理", actionType = ActionType.DELETE)
    ////@RequiresPermissions(value = "system:role:delete")
    public ActionResult<Void> delete(@Validated @NotEmpty(message = "id不能为空") @RequestBody List<String> ids) {
        if (sysRoleService.removeByIds(ids) > 0) {
            return ActionResult.success("删除成功");
        }
        return ActionResult.fail("删除失败");
    }


}
